package com.yu.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yu.bean.Employee;
import com.yu.bean.Project;
import com.yu.service.EmployeeService;
import com.yu.service.ProjectService;

@Controller
@RequestMapping("/project")
public class ProjectController {
	
	@Autowired
	private ProjectService projectService;

	@Autowired
	private EmployeeService employeeService;

	@RequestMapping("/getAll")
	public String getAll(Model model) {
		List<Project> list = projectService.getAllWithEmployee();
		model.addAttribute("list", list);
		return "project/showList";
	}

	@RequestMapping("/toAdd")
	public String toAdd(Model model) {
		List<Employee> list = employeeService.getAll();
		model.addAttribute("employeeList", list);
		return "project/add";
	}

	@RequestMapping("/add")
	public String add(Project project) {
		projectService.add(project);
		return "forward:/project/getAll.do";
	}

	@RequestMapping("/delete")
	public String delete(int id) {
		projectService.delete(id);
		return "forward:/project/getAll.do";
	}

	@RequestMapping("/toEdit")
	public String toEdit(Model model, int id) {
		Project project = projectService.getById(id);
		model.addAttribute("project", project);
		List<Employee> list = employeeService.getAll();
		model.addAttribute("employeeList", list);
		return "project/edit";
	}

	@RequestMapping("/edit")
	public String edit(Project project) {
		projectService.update(project);
		return "forward:/project/getAll.do";
	}
}
